<?php
namespace Operationtest\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;

class MarcheController extends AbstractActionController
{
    protected $marcheTable;
    protected $storeTable;
    protected $marchestoreTable;
    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/operationtest');
        }, 100);
 
        return $this;
    }
    
    public function getMarcheTable()
    {
        if (!$this->marcheTable) {
            $sm = $this->getServiceLocator();
            $this->marcheTable = $sm->get('Operationtest\Model\MarcheTable');
        }
        return $this->marcheTable;
    }
    
    public function getStoreTable()
    {
        if (!$this->storeTable) {
            $sm = $this->getServiceLocator();
            $this->storeTable = $sm->get('Operationtest\Model\StoreTable');
        }
        return $this->storeTable;
    }
    
    public function getMarchestoreTable()
    {
        if (!$this->marchestoreTable) {
            $sm = $this->getServiceLocator();
            $this->marchestoreTable = $sm->get('Operationtest\Model\MarchestoreTable');
        }
        return $this->marchestoreTable;
    }
    
    public function indexAction()
    {
        return new ViewModel(array(
            'marches' => $this->getMarcheTable()->fetchAll(),
        ));
    }
    
    public function savenewAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $idMarche = '';
            $libelle = ($request->getPost()->libelle)?$request->getPost()->libelle:'';
            $description = ($request->getPost()->description)?$request->getPost()->description:'';
            $active = ($request->getPost()->active)?$request->getPost()->active:'';
        }
        
        $this->getMarcheTable()->saveNew($idMarche,$libelle,$description,$active);

        $viewModel = new ViewModel(array(
            'message' => 'Nouveau marché enregistré',
        ));

        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function updateAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            
            $pk = $request->getPost()->pk;
            $name = $request->getPost()->name;
            $value = $request->getPost()->value;
            
            $this->getMarcheTable()->xeditUpdate($pk, $name, $value);
        }
    }
    
    public function deleteAction()
    {
        $id = $this->params('id');
        $this->getMarcheTable()->deleteMarche($id);
        return $this->redirect()->toRoute('operationtest/default', array('controller'=>'marche'));
    }
    
    public function attribstoreAction() {
        $idMarche = $this->params('id');
        $request = $this->getRequest();
        $allstores = $myallstores = $myallnewstores = array(); 
        
        $mystores = $this->getMarchestoreTable()->getMarcheStore($idMarche);
        foreach($mystores as $mystore) {
            $myallstores[] = $mystore['idStore'];
        }
        
        if ($request->getPost()->selectable) {
            $rows = $request->getPost()->selectable;
            
            // Ajout et mis en tableau des nouvelles sélections
            foreach($rows as $row) {
                if(!in_array($row, $myallstores))
                    $this->getMarchestoreTable()->attribMarcheStore("", $idMarche, $row);
                $myallnewstores[] = $row;
            }
            
            // Elimination des boutiques non présents dans les nouvelles sélections
            foreach($myallstores as $oldstore) {
                if(!in_array($oldstore, $myallnewstores))
                    $this->getMarchestoreTable()->deleteMarcheStore($idMarche, $oldstore);    
            }
            
            return $this->redirect()->toRoute('operationtest/default', array('controller'=>'marche'));
        } else {
            if($request->getPost()->submit) {
                foreach($myallstores as $oldstore) {
                    $this->getMarchestoreTable()->deleteMarcheStore($idMarche, $oldstore);    
                } 
            }
        }
        
        $allstores = $this->getStoreTable()->fetchAll();
        
        return new ViewModel(array(
            'marche' => $this->getMarcheTable()->getMarche($idMarche),
            'allstores' => $allstores,
            'myallstores' => $myallstores,
        ));
    }
}